﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace XCrawler
{
    public static class CrawlerExtensions
    {

        #region 【爬虫事件操作】

        /// <summary>
        /// 开始爬行回调
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="OnStart"></param>
        /// <returns></returns>
        public static ICrawler OnStart(this ICrawler Crawler, EventHandler<OnStartEventArgs> OnStart)
        {
            Crawler.OnStartEvent += OnStart;
            return Crawler;
        }

        /// <summary>
        /// 爬行成功回调
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="OnCompleted"></param>
        /// <returns></returns>
        public static ICrawler OnCompleted(this ICrawler Crawler, EventHandler<OnCompletedEventArgs> OnCompleted)
        {
            Crawler.OnCompletedEvent += OnCompleted;
            return Crawler;
        }

        /// <summary>
        /// 爬行失败回调
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="OnError"></param>
        /// <returns></returns>
        public static ICrawler OnError(this ICrawler Crawler, EventHandler<OnErrorEventArgs> OnError)
        {
            Crawler.OnErrorEvent += OnError;
            return Crawler;
        }

        #endregion


        #region 【爬虫快捷操作】

       

        /// <summary>
        /// 创建爬虫
        /// </summary>
        /// <param name="CrawlerUrl"></param>
        /// <returns></returns>
        public static ICrawler CreateCrawler(this string CrawlerUrl, params ICrawlerParser[] parsers)
        {
            return CrawlerUrl.CreateCrawler(new HttpCrawler(), parsers);
        }

 

        /// <summary>
        /// 创建爬虫
        /// </summary>
        /// <param name="CrawlerUrl">需要爬行的地址</param>
        /// <param name="Crawler">爬虫的类型</param>
        /// <param name="parsers">解析器集合</param>
        /// <returns></returns>
        public static ICrawler CreateCrawler(this string CrawlerUrl,ICrawler Crawler, params ICrawlerParser[] parsers)
        {
            Crawler.CrawlerUrl = new Uri(CrawlerUrl);
            Crawler.Parsers = parsers;
            return Crawler;
        }

        /// <summary>
        /// 设置解析器集合
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="parsers"></param>
        /// <returns></returns>
        public static ICrawler SetParsers(this ICrawler Crawler, params ICrawlerParser[] parsers)
        {
            Crawler.Parsers = parsers;
            return Crawler;
        }

        /// <summary>
        /// 增加单个解析器
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="parsers"></param>
        /// <returns></returns>
        public static ICrawler AddParsers(this ICrawler Crawler,ICrawlerParser parser)
        {
            if(!(Crawler.Parsers != null && Crawler.Parsers.Count() > 0))
            {
                Crawler.Parsers = new List<ICrawlerParser>();
            }
            Crawler.Parsers.Add(parser);
            return Crawler;
        }



        /// <summary>
        /// 设置请求方式
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="Method"></param>
        /// <returns></returns>
        public static ICrawler SetMethod(this ICrawler Crawler, string Method = "GET")
        {
            Crawler.Method = Method;
            return Crawler;
        }

        /// <summary>
        /// 设置请求URL
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="CrawlerUrl"></param>
        /// <returns></returns>
        public static ICrawler SetUrl(this ICrawler Crawler, string CrawlerUrl)
        {
            Crawler.CrawlerUrl = new Uri(CrawlerUrl);
            return Crawler;
        }

        /// <summary>
        /// 设置Cookie
        /// 为了保持登录状态,第二次设置的时候如果原来已有返回Cookie则不能再次设置
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="CookiesContainer"></param>
        /// <returns></returns>
        public static ICrawler SetCookie(this ICrawler Crawler, string Cookies)
        {
            if(!Cookies.IsNullOrWhiteSpace())
            {
                Crawler.Cookies = Cookies;
            }
            return Crawler;
        }

 

        /// <summary>
        /// 开始爬取工作（异步）
        /// </summary>
        /// <param name="Crawler"></param>
        /// <returns></returns>
        public static async Task<ICrawler> ToWorkerAsync(this ICrawler Crawler)
        {
            await Crawler.StartAsync();
            return Crawler;
        }

        #endregion



    }
}
